﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:model="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable"
    exclude-result-prefixes="msxsl">

  <xsl:output method="xml" indent="yes" />
  
  <xsl:template match="model:CommandTable" >
    <xsl:element name="CommandTable" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
      <xsl:attribute name="language">
        <xsl:value-of select="@language"/>
      </xsl:attribute>
      <xsl:comment>
        // Generated comment - Do not modify or move
        // customGenerator={<xsl:value-of select="@customToolGenerator"/>}
    </xsl:comment>
      <xsl:apply-templates select="model:Extern"/>
      <xsl:apply-templates select="model:Include"/>
      <xsl:apply-templates select="model:Commands"/>
      <xsl:apply-templates select="model:Define"/>

      <xsl:if test="count(model:Commands//model:ParentMenu) + count(model:Commands//model:ParentGroup) + count(model:Commands//model:ParentExtern) > 0">
        <xsl:element name="CommandPlacements" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
          <xsl:for-each select="model:Commands//model:ParentExtern|model:Commands//model:ParentGroup|model:Commands//model:ParentMenu">
            <xsl:call-template name="EmitPlacement"/>
          </xsl:for-each>
        </xsl:element>
      </xsl:if>
      
      <xsl:if test="count(/model:Commands//model:VisibilityContext) > 0 and string-length(text()) > 0">
        <xsl:element name="VisibilityConstraints" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
          <xsl:for-each select="/model:Commands//model:VisibilityContext">
            <xsl:element name="VisibilityItem" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
              <xsl:attribute name="guid">
                <xsl:value-of select="parent::node()/@guid"/>
              </xsl:attribute>
              <xsl:attribute name="id">
                <xsl:value-of select="parent::node()/@id"/>
              </xsl:attribute>
              <xsl:attribute name="context">
                <xsl:value-of select="text()"/>
              </xsl:attribute>
            </xsl:element>
          </xsl:for-each>
        </xsl:element>
      </xsl:if>
      
      <xsl:if test="count(model:Commands//model:KeyBinding) > 0">
        <xsl:element name="KeyBindings" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
          <xsl:for-each select="model:Commands//model:KeyBinding">
            <xsl:element name="KeyBinding" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
              <xsl:attribute name="guid">
                <xsl:value-of select="parent::node()/@guid"/>
              </xsl:attribute>
              <xsl:attribute name="id">
                <xsl:value-of select="parent::node()/@id"/>
              </xsl:attribute>
              <xsl:attribute name="editor">
                <xsl:value-of select="@editor"/>
              </xsl:attribute>
              <xsl:attribute name="key1">
                <xsl:value-of select="@key1"/>
              </xsl:attribute>
              <xsl:attribute name="mod1">
                <xsl:value-of select="@mod1"/>
              </xsl:attribute>
              <xsl:attribute name="key2">
                <xsl:value-of select="@key2"/>
              </xsl:attribute>
              <xsl:attribute name="mod2">
                <xsl:value-of select="@mod2"/>
              </xsl:attribute>
              <xsl:attribute name="emulator">
                <xsl:value-of select="@emulator"/>
              </xsl:attribute>
            </xsl:element>
          </xsl:for-each>
        </xsl:element>
      </xsl:if>
      <xsl:apply-templates select="model:UsedCommands"/>
      <xsl:apply-templates select="model:Symbols"/>

    </xsl:element>
  </xsl:template>

  <xsl:template match="model:Icon">
    <xsl:element name="Icon" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" >
      <xsl:attribute name="guid">
        <xsl:value-of select="substring-after(model:commandHasIcon/model:bitmapMoniker/@guid, '1/')"/>
      </xsl:attribute>
      <xsl:attribute name="id">
        <xsl:value-of select="model:commandHasIcon/@iconId"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <xsl:template match="model:externCommandAliases"/>
  <xsl:template match="model:KeyBinding"/>
  <xsl:template match="model:VisibilityContext"/>
  <xsl:template match="model:Bitmaps"/>
  <xsl:template match="model:ParentGroup|model:ParentMenu|model:ParentExtern"/>
  
  <xsl:template match="model:Menu|model:Button|model:Group|model:Combo">
    <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="model:Icon"/>
    <xsl:apply-templates select="model:CommandFlag"/>
    <xsl:apply-templates select="model:Strings"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="model:Commands">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"></xsl:apply-templates>
      
      <xsl:if test="count(/model:CommandTable/model:Bitmaps/model:Bitmap) > 0">
      <xsl:element name="Bitmaps" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
        <xsl:apply-templates select="/model:CommandTable/model:Bitmaps/model:Bitmap"></xsl:apply-templates>
      </xsl:element>
    </xsl:if>
    </xsl:copy>    
  </xsl:template>

  <xsl:template name="EmitPlacement">
    <xsl:element name="CommandPlacement" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
      <xsl:attribute name="guid">
        <xsl:value-of select="parent::node()/@guid"/>
      </xsl:attribute>
      <xsl:attribute name="id">
        <xsl:value-of select="parent::node()/@id"/>
      </xsl:attribute>
      <xsl:element name="Parent" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
        <xsl:attribute name="guid">
          <xsl:value-of select="substring-before( substring-after(@id, '/'), '/')"/>
        </xsl:attribute>
        <xsl:attribute name="id">
          <xsl:value-of select="substring-after(substring-after(@id, '/'), '/')"/>
        </xsl:attribute>
      </xsl:element>
    </xsl:element>
  </xsl:template>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()">
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
